from functools import reduce
from collections import Iterable


def coll2str(coll):
    """
    集合转化成逗号分割的字符串，主要用于API传参
    :param coll:
    :return:
    """
    if isinstance(coll, Iterable) and not isinstance(coll, str):
        if len(coll) > 1:
            return reduce(lambda x, y: "{},{}".format(x, y), coll)
        elif len(coll) == 1:
            return coll[0]
        else:
            return ''
    return coll


class FilePool(object):
    """
    文件池
    """

    def __init__(self, paths):
        self.files = list(map(lambda name: open(name, 'rb'), paths))

    def __enter__(self):
        return self.files

    def __exit__(self, exc_type, exc_val, exc_tb):
        for f in self.files:
            f.close()
